package com.example.news.config;

import entity.HttpCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiongzh on 2018/3/21.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    SystemConfig systemConfig;

    @Value("${swagger.packageScan}")
    String packageScan;

    @Bean
    public Docket docket(){
        ParameterBuilder ticketPar1 = new ParameterBuilder();
        ParameterBuilder ticketPar2 = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar1.name("roleSign").description("用户角色标志")
                .modelRef(new ModelRef("string")).parameterType("roleSign")
                .required(false).build(); //header中的参数非必填，传空也可以
        ticketPar2.name("userId").description("用户Id")
                .modelRef(new ModelRef("string")).parameterType("userId")
                .required(false).build(); //header中的参数非必填，传空也可以
        pars.add(ticketPar1.build());
        pars.add(ticketPar2.build());

        List<ResponseMessage> responseMessageList = new ArrayList<>();
        for (HttpCode httpCode : HttpCode.values()) {
            responseMessageList.add(new ResponseMessageBuilder().code(httpCode.getCode()).message(httpCode.getMsg()).build());
        }


        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .apiInfo(apiInfo()).select()
//                   当前包路径
                .apis(RequestHandlerSelectors.basePackage(packageScan))
//                   .apis(RequestHandlerSelectors.basePackage("com.example.demo.common"))  // 将loginController 移到common中
                .paths(PathSelectors.any()).build().globalOperationParameters(pars);


    }


    //构建api文档的详细信息函数
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //页面标题
                .title(systemConfig.getSwaggerTitle())
                //创建人
                .contact(new Contact(systemConfig.getSwaggerContactName(),
                        systemConfig.getSwaggerContactWebUrl(),
                        systemConfig.getSwaggerContactEmail()))
                //版本号
                .version(systemConfig.getSwaggerVersion())
                //描述
                .description(systemConfig.getSwaggerDescription())
                .termsOfServiceUrl(systemConfig.getSwaggerTermsOfServiceUrl())
                .build();
    }
}

